//
//  BKMyOrderBottomView.m
//  bestkeep
//
//  Created by Mr. Wu on 16/12/22.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKMyOrderBottomView.h"
#import "UIButton+BKMyOrderButton.h"
#import "BK_OD_Tool.h"
#import "BK_OD_Color.h"
@implementation BKMyOrderBottomView

-(instancetype)initWithFrame:(CGRect)frame{
    self =  [super initWithFrame:frame];
    
    if (self) {
        [self initView];
    }
    
    return self;
}
-(void)initView{
    _lab_payTime = [UILabel new];
    _lab_payTime.text = @"付款剩余时间:";
    _lab_payTime.font = [UIFont systemFontOfSize:12.0];
    _lab_payTime.backgroundColor = [UIColor whiteColor];
    _lab_payTime.textColor = kOrderBlack1;
    [self addSubview:_lab_payTime];
    
    
    _lab_time = [[UILabel alloc]initWithFrame:CGRectMake(5, 25, 150, 21)];
    _lab_time.textColor = kOrderBlack1;
    _lab_time.text = @"0小时0分钟0秒";
    _lab_time.font = [UIFont systemFontOfSize:12.0];
    _lab_time.backgroundColor = [UIColor whiteColor];
    [self addSubview:_lab_time];
    [_lab_time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.mas_bottom).offset(-8);
        make.left.mas_equalTo(self.mas_left).offset(ApplicationBackGauge);
    }];
    
    [_lab_payTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_lab_time.mas_top).offset(-1);
        make.left.mas_equalTo(self.mas_left).offset(ApplicationBackGauge);
    }];
    
    _lab_payTime.hidden=YES;
    _lab_time.hidden=YES;
    _btn_right_one = [UIButton buttonWithTitle:@"1"];
    [self addSubview:_btn_right_one];
    _btn_right_two = [UIButton buttonWithTitle:@"加载中"];
    [self addSubview:_btn_right_two];
    _btn_right_three = [UIButton buttonWithTitle:@"加载中"];
    [self addSubview:_btn_right_three];
    _btn_right_four = [UIButton buttonWithTitle:@"加载中"];
    [self addSubview:_btn_right_four];
    [_btn_right_one mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-ApplicationBackGauge);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.width.mas_equalTo(@(ScreenWidth/4 -20));
        make.height.mas_equalTo(@(27));
    }];
    
    [_btn_right_two mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_btn_right_one.mas_left).offset(-ApplicationBackGauge);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.width.mas_equalTo(@(ScreenWidth/4 -20));
        make.height.mas_equalTo(@(27));
    }];
    
    [_btn_right_three mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_btn_right_two.mas_left).offset(-ApplicationBackGauge);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.width.mas_equalTo(@(ScreenWidth/4 -20));
        make.height.mas_equalTo(@(27));
    }];
    
    [_btn_right_four mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_btn_right_three.mas_left).offset(-ApplicationBackGauge);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.width.mas_equalTo(@(ScreenWidth/4 -20));
        make.height.mas_equalTo(@(27));
    }];
    
    
    
}
-(void)setOrderModel:(BK_My_Order_rows_Model *)orderModel{
    _orderModel = orderModel;
    
    
    if ([orderModel.status isEqualToString:@"01"]) {
        [_btn_right_one setTitle:@"去付款" forState:UIControlStateNormal];
        
    }else if([orderModel.status isEqualToString:@"02"]){
        /**
         *  即便是已支付状态   仍然要判断是否显示【取消订单】
         */
        /*
         * own_calcle_flag   own_cancel_flag
         */
        if ([orderModel.own_cancel_flag isEqualToString:@"1"]) {
            [_btn_right_one setTitle:@"取消订单" forState:UIControlStateNormal];
            _btn_right_one.layer.borderColor = BORDERCOLOR_GRAY.CGColor;
            [_btn_right_one setTitleColor:TEXTCOLOR_GRAY forState:UIControlStateNormal];
        }
    }else if([orderModel.status isEqualToString:@"03"]){
        [_btn_right_one setTitle:@"确认收货" forState:UIControlStateNormal];
        [_btn_right_two setTitle:@"查看物流" forState:UIControlStateNormal];
        if (orderModel.isShow_Return_Goods) {//是否显示退换货
            [_btn_right_three setTitle:@"退换货" forState:UIControlStateNormal];
        }
    }else if([orderModel.status isEqualToString:@"04"]){
        
        if ([orderModel.evaluation_flag  isEqualToString:@"1"]) {
            [_btn_right_one setTitle:@"去评价" forState:UIControlStateNormal];
            [_btn_right_two setTitle:@"查看物流" forState:UIControlStateNormal];
            [_btn_right_three setTitle:@"删除订单" forState:UIControlStateNormal];
        }else{
            [_btn_right_one setTitle:@"查看物流" forState:UIControlStateNormal];
            [_btn_right_two setTitle:@"删除订单" forState:UIControlStateNormal];
            _btn_right_one.layer.borderColor = BORDERCOLOR_GRAY.CGColor;
            [_btn_right_one setTitleColor:TEXTCOLOR_GRAY forState:UIControlStateNormal];
        }
    }else if([orderModel.status isEqualToString:@"05"]){
        [_btn_right_one setTitle:@"删除订单" forState:UIControlStateNormal];
        _btn_right_one.layer.borderColor = BORDERCOLOR_GRAY.CGColor;
        [_btn_right_one setTitleColor:TEXTCOLOR_GRAY forState:UIControlStateNormal];
    }else if([orderModel.status isEqualToString:@"06"]){
        [_btn_right_one setTitle:@"删除订单" forState:UIControlStateNormal];
        _btn_right_one.layer.borderColor = BORDERCOLOR_GRAY.CGColor;
        [_btn_right_one setTitleColor:TEXTCOLOR_GRAY forState:UIControlStateNormal];
    }else if([orderModel.status isEqualToString:@"07"]){
        [_btn_right_one setTitle:@"删除订单" forState:UIControlStateNormal];
        _btn_right_one.layer.borderColor = BORDERCOLOR_GRAY.CGColor;
        [_btn_right_one setTitleColor:TEXTCOLOR_GRAY forState:UIControlStateNormal];
    }else{
    }
    if([_btn_right_one.titleLabel.text rangeOfString:@"加载中"].location !=NSNotFound){
        _btn_right_one.hidden=YES;
    }
    if([_btn_right_two.titleLabel.text rangeOfString:@"加载中"].location !=NSNotFound){
        _btn_right_two.hidden=YES;
    }
    if([_btn_right_three.titleLabel.text rangeOfString:@"加载中"].location !=NSNotFound){
        _btn_right_three.hidden=YES;
    }
    if([_btn_right_four.titleLabel.text rangeOfString:@"加载中"].location !=NSNotFound){
        _btn_right_four.hidden=YES;
    }
    
    
}
-(void)setOrder_model:(BK_My_DetailOrder_Model *)order_model{
    _order_model = order_model;
    /*
     _str_status 订单状态：
     01，待支付                     去付款
     02，已支付（待发货）    取消订单
     03，已发货                     查看物流 ，收货确认
     04，交易成功                  删除订单，查看物流，去评价
     05，交易关闭
     06，已取消
     07，已退款
     */
    // 04：交易成功 05：交易关闭，06：取消订单，07：退款成功   列表和订单详情添加删除订单按钮
    NSString *_str_status = order_model.order_status;
    
    if ([_str_status isEqualToString:@"01"]){
        _btn_right_one.hidden=NO;
        [_btn_right_one setTitle:@"去付款" forState:UIControlStateNormal];
        self.long_time = [order_model.canPayTimes floatValue];
        
        [self action_addTime];
    }else if([_str_status isEqualToString:@"02"]){
        if ([order_model.own_cancel_flag isEqualToString:@"0"]){
            //不显示取消订单
        }else if([order_model.own_cancel_flag isEqualToString:@"1"]){
            //显示取消订单
            _btn_right_one.hidden=NO;
            [_btn_right_one setTitle:@"取消订单" forState:UIControlStateNormal];
        }
    }else if([_str_status isEqualToString:@"03"]){
        _btn_right_one.hidden=NO;
        _btn_right_two.hidden=NO;
        [_btn_right_one setTitle:@"确认收货" forState:UIControlStateNormal];
        [_btn_right_two setTitle:@"查看物流" forState:UIControlStateNormal];
        BOOL  show_reutnGoogd = false;  //如果子商品中有未退货,显示未退货;
        for (BK_My_DetailOrder_OrderList_Model*list in order_model.order_list) {
            if ([list.returnFlag isEqualToString:@"0"]) {
                show_reutnGoogd = YES;
            }
        }
        
        if (show_reutnGoogd){
            _btn_right_three.hidden=NO;
            [_btn_right_three setTitle:@"退换货" forState:UIControlStateNormal];
        }else{
            _btn_right_three.hidden=YES;
        }
    }else if([_str_status isEqualToString:@"04"]){
        //evaluationFlag  //0 未评价 1 部分评价 2全部评价
        if ([order_model.evaluationFlag integerValue]!=2){
            _btn_right_four.hidden=YES;
            [_btn_right_one setTitle:@"去评价" forState:UIControlStateNormal];
            [_btn_right_two setTitle:@"查看物流" forState:UIControlStateNormal];
            [_btn_right_three setTitle:@"删除订单" forState:UIControlStateNormal];
        }else{
            _btn_right_three.hidden=YES;
            _btn_right_four.hidden=YES;
            [_btn_right_one setTitle:@"查看物流" forState:UIControlStateNormal];
            [_btn_right_two setTitle:@"删除订单" forState:UIControlStateNormal];
        }
    }else if([_str_status isEqualToString:@"05"]){
        [_btn_right_one setTitle:@"删除订单" forState:UIControlStateNormal];
    }else if([_str_status isEqualToString:@"06"]){
        [_btn_right_one setTitle:@"删除订单" forState:UIControlStateNormal];
    }else if([_str_status isEqualToString:@"07"]){
        [_btn_right_one setTitle:@"删除订单" forState:UIControlStateNormal];
    }
    
    //当用户订单中商品数量大于一件   显示拆分订单
    
    if ([order_model.splitFlag isEqualToString:@"1"]&&order_model.order_amount_lable_list.count>1){
        if ([_btn_right_one.titleLabel.text  rangeOfString:@"加载中"].location!=NSNotFound){
            [_btn_right_one setTitle:@"拆单" forState:UIControlStateNormal];
        }else if([_btn_right_two.titleLabel.text  rangeOfString:@"加载中"].location!=NSNotFound){
            [_btn_right_two setTitle:@"拆单" forState:UIControlStateNormal];
        }else if([_btn_right_three.titleLabel.text  rangeOfString:@"加载中"].location!=NSNotFound){
            [_btn_right_three setTitle:@"拆单" forState:UIControlStateNormal];
        }else{
            [_btn_right_four setTitle:@"拆单" forState:UIControlStateNormal];
        }
    }
    if([_btn_right_one.titleLabel.text rangeOfString:@"加载中"].location !=NSNotFound){
        _btn_right_one.hidden=YES;
    }
    if([_btn_right_two.titleLabel.text rangeOfString:@"加载中"].location !=NSNotFound){
        _btn_right_two.hidden=YES;
    }
    if([_btn_right_three.titleLabel.text rangeOfString:@"加载中"].location !=NSNotFound){
        _btn_right_three.hidden=YES;
    }
    if([_btn_right_four.titleLabel.text rangeOfString:@"加载中"].location !=NSNotFound){
        _btn_right_four.hidden=YES;
    }
    
    [self relayOutFooterBtn];
}

-(void)action_addTime{
    
    _lab_payTime.hidden=NO;
    _lab_time.hidden=NO;
    _timer_move = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(action_animal_MoveTime) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop]addTimer:_timer_move forMode:NSRunLoopCommonModes];
    
}
-(void)action_animal_MoveTime{
    
    if (_long_time > 0){
        
        _long_time--;
        long hour = _long_time / 3600;
        long minutes = (_long_time-3600*hour)/60;
        long miao  = _long_time%60;
        if (_long_time == 0){
            
            [_timer_move invalidate];
            _timer_move = nil;
            _btn_right_one.userInteractionEnabled = NO;
            _btn_right_one.layer.borderColor = BORDERCOLOR_GRAY.CGColor;
            [_btn_right_one setTitleColor:TEXTCOLOR_GRAY forState:UIControlStateNormal];
        }
        
        NSString *t = [NSString stringWithFormat:@"%ld小时%ld分钟%ld秒", hour,minutes,miao];
        _lab_time.text = t;
        
    }else{
        
        [_timer_move invalidate];
        _btn_right_one.userInteractionEnabled = NO;
        _btn_right_one.layer.borderColor = BORDERCOLOR_GRAY.CGColor;
        [_btn_right_one setTitleColor:TEXTCOLOR_GRAY forState:UIControlStateNormal];
    }
    
}
-(void)relayOutFooterBtn{
    
    if ([_btn_right_one.titleLabel.text  rangeOfString:@"取消订单"].location!=NSNotFound){
        _btn_right_one.layer.borderColor = BORDERCOLOR_GRAY.CGColor;
        [_btn_right_one setTitleColor:TEXTCOLOR_GRAY forState:UIControlStateNormal];
    }
    
    if ([_btn_right_one.titleLabel.text  rangeOfString:@"删除订单"].location!=NSNotFound){
        _btn_right_one.layer.borderColor = BORDERCOLOR_GRAY.CGColor;
        [_btn_right_one setTitleColor:TEXTCOLOR_GRAY forState:UIControlStateNormal];
    }
    
    if ([_btn_right_one.titleLabel.text  rangeOfString:@"查看物流"].location!=NSNotFound){
        _btn_right_one.layer.borderColor = BORDERCOLOR_GRAY.CGColor;
        [_btn_right_one setTitleColor:TEXTCOLOR_GRAY forState:UIControlStateNormal];
    }
    
}
-(void)dealloc{
    
    [ self.timer_move invalidate];
    
}
@end

















